package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.SignDetail;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SignDetailMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SignServiceImpl implements ISignService {

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SignDetailMapper signDetailMapper;

    //保存签到对象
    public void saveSign(Sign sign) {
            //判断今天是否签到
            if(getSignState(sign)==null){

                //用户每次签到赠送5积分
                User user = UserContext.getCurrentUser();
                sign.setUser(user);
                //创建时间
                Date date = new Date();
                sign.setSignTime(date);
                //设置签到时间
                sign.setState(1);
                user.setIntegral(user.getIntegral()+5);

                //保存签到明细
                SignDetail signDetail=new SignDetail();
                signDetail.setCreteTime(date);
                signDetail.setUserId(user.getId());

                //判断昨天是否签到(没签到)
                if(getLastDay(sign)==null){
                    sign.setSignCount(1);
                };

                //如果昨天已签到
                if(getLastDay(sign)!=null){
                    //如果连签次数小于3次,那么连签次数+1
                    if(sign.getSignCount()<3){
                        sign.setSignCount(getLastDay(sign).getSignCount()+1);
                    };
                    //如果连签次数为3次,那么额外赠送20积分,连签中断
                    if(sign.getSignCount()==3){
                        user.setIntegral(user.getIntegral()+20);
                        sign.setSignCount(1);
                    };
                };
                signDetailMapper.insert(signDetail);

                //如果有签到记录则修改,没有记录就新增
                if(getSign(sign)==null){
                    signMapper.insert(sign);
                }else{
                    signMapper.updateByPrimaryKey(sign);
                }
                userMapper.updateIntegral(user);
            }else{
                throw new RuntimeException("今天已经签到");
            }


    }

    public List<SignDetail> getAllSignDetailByUserId(Long id) {
       return  signDetailMapper.selectByPrimaryKey(id);
    }

    //查询签到对象信息
    public Sign getSign(Long id) {
        return signMapper.selectListById(id);
    }

    //查询当天是否签到
    public Sign getSignState(Sign sign){
        //获取到当前时间
        return signMapper.selectByStateAndTime(sign.getUser().getId());
    }

    //查询昨天是否签到
    public Sign getLastDay(Sign sign){
        //获取到当前时间
        return signMapper.selectLastDay(sign.getUser().getId());
    }

    //查询用户是否有过签到记录
    public Sign getSign(Sign sign){
        //获取到当前时间
        return signMapper.selectListById(sign.getUser().getId());
    }



}
